/**
 * 
 */
package com.aaron.client;

import java.util.Iterator;

import com.google.gwt.user.client.ui.Composite;

import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IndexedPanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.WidgetCollection;
import com.google.gwt.user.client.ui.TabListenerCollection;

import com.bouwkamp.gwt.user.client.ui.RoundedPanel;

/**
 * @author aaron
 *
 */
public class RoundedTabPanel extends Composite implements TabListener,
SourcesTabEvents, HasWidgets, IndexedPanel {

	private WidgetCollection children = new WidgetCollection(this);
	private DeckPanel deck = new DeckPanel();
	private RoundedTabBar tabBar = new RoundedTabBar();
	//wrap the deck in a rounded panel
	private RoundedPanel roundedDeck=new RoundedPanel(deck);
	private TabListenerCollection tabListeners;

	/**
	 * Creates an empty tab panel.
	 */
	public RoundedTabPanel() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(tabBar);
		panel.add(roundedDeck);

		panel.setCellHeight(deck, "100%");
		tabBar.setWidth("100%");

		tabBar.addTabListener(this);
		initWidget(panel);
		setStyleName("gwt-TabPanel");
		deck.setStyleName("gwt-TabPanelBottom");
	}

	public void add(Widget w) {
		throw new UnsupportedOperationException(
		"A tabText parameter must be specified with add().");
	}

	/**
	 * Adds a widget to the tab panel.
	 * 
	 * @param w the widget to be added
	 * @param tabText the text to be shown on its tab
	 */
	public void add(Widget w, String tabText) {
		insert(w, tabText, getWidgetCount());
	}

	/**
	 * Adds a widget to the tab panel.
	 * 
	 * @param w the widget to be added
	 * @param tabText the text to be shown on its tab
	 * @param asHTML <code>true</code> to treat the specified text as HTML
	 */
	public void add(Widget w, String tabText, boolean asHTML) {
		insert(w, tabText, asHTML, getWidgetCount());
	}

	/**
	 * Adds a widget to the tab panel.
	 * 
	 * @param w the widget to be added
	 * @param tabWidget the widget to be shown in the tab
	 */
	//removed because this is newer than the current jar build
	public void add(Widget w, Widget tabWidget) {
		insert(w, tabWidget, getWidgetCount());
	}

	public void addTabListener(TabListener listener) {
		if (tabListeners == null) {
			tabListeners = new TabListenerCollection();
		}
		tabListeners.add(listener);
	}

	public void clear() {
		while (getWidgetCount() > 0) {
			remove(getWidget(0));
		}
	}

	/**
	 * Gets the deck panel within this tab panel.
	 * 
	 * @return the deck panel
	 */
	public DeckPanel getDeckPanel() {
		return deck;
	}

	/**
	 * Gets the tab bar within this tab panel.
	 * 
	 * @return the tab bar
	 */
	public RoundedTabBar getTabBar() {
		return tabBar;
	}

	public Widget getWidget(int index) {
		return children.get(index);
	}

	public int getWidgetCount() {
		return children.size();
	}

	public int getWidgetIndex(Widget widget) {
		return children.indexOf(widget);
	}

	/**
	 * Inserts a widget into the tab panel.
	 * 
	 * @param widget the widget to be inserted
	 * @param tabText the text to be shown on its tab
	 * @param asHTML <code>true</code> to treat the specified text as HTML
	 * @param beforeIndex the index before which it will be inserted
	 */
	public void insert(Widget widget, String tabText, boolean asHTML,
			int beforeIndex) {
		children.insert(widget, beforeIndex);
		tabBar.insertTab(tabText, asHTML, beforeIndex);
		deck.insert(widget, beforeIndex);
	}

	/**
	 * Inserts a widget into the tab panel.
	 * 
	 * @param widget the widget to be inserted.
	 * @param tabWidget the widget to be shown on its tab.
	 * @param beforeIndex the index before which it will be inserted.
	 */
	public void insert(Widget widget, Widget tabWidget, int beforeIndex) {
		children.insert(widget, beforeIndex);
		tabBar.insertTab(tabWidget, beforeIndex);
		deck.insert(widget, beforeIndex);
	}

	/**
	 * Inserts a widget into the tab panel.
	 * 
	 * @param widget the widget to be inserted
	 * @param tabText the text to be shown on its tab
	 * @param beforeIndex the index before which it will be inserted
	 */
	public void insert(Widget widget, String tabText, int beforeIndex) {
		insert(widget, tabText, false, beforeIndex);
	}

	public Iterator iterator() {
		return children.iterator();
	}

	public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
		if (tabListeners != null) {
			return tabListeners.fireBeforeTabSelected(this, tabIndex);
		}
		return true;
	}

	public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
		deck.showWidget(tabIndex);
		if (tabListeners != null) {
			tabListeners.fireTabSelected(this, tabIndex);
		}
	}

	public boolean remove(int index) {
		return remove(getWidget(index));
	}

	/**
	 * Removes the given widget, and its associated tab.
	 * 
	 * @param widget the widget to be removed
	 */
	public boolean remove(Widget widget) {
		int index = getWidgetIndex(widget);
		if (index == -1) {
			return false;
		}

		children.remove(widget);
		tabBar.removeTab(index);
		deck.remove(widget);
		return true;
	}

	public void removeTabListener(TabListener listener) {
		if (tabListeners != null) {
			tabListeners.remove(listener);
		}
	}

	/**
	 * Programmatically selects the specified tab.
	 * 
	 * @param index the index of the tab to be selected
	 */
	public void selectTab(int index) {
		tabBar.selectTab(index);
	}
}
